package cn.edu.zafu.bigdata.Wym;

import java.io.*;
import java.util.*;

public class Wym_StudentManagementSystem {
    private static final String FILE_NAME = "students.dat";
    private Map<String, Wym_Student> studentMap = new HashMap<String, Wym_Student>();
    private Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        Wym_StudentManagementSystem system = new Wym_StudentManagementSystem();
        system.loadStudents();
        system.showMenu();
    }

    private void showMenu() {
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除换行符
            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    searchStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    saveStudents();
                    System.out.println("退出程序。");
                    return;
                default:
                    System.out.println("无效的操作编号，请重新输入。");
            }
        }
    }

    private void addStudent() {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        if (studentMap.containsKey(id)) {
            System.out.println("学号已存在，请重新输入。");
            return;
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        Wym_Student student = new Wym_Student(id, name, phone);
        studentMap.put(id, student);
        System.out.println("学生信息添加成功。");
    }

    private void searchStudent() {
        while (true) {
            System.out.println("请输入要检索的学生学号（输入QUIT退出）：");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }

            Wym_Student student = studentMap.get(id);
            if (student != null) {
                System.out.println("学生信息：");
                System.out.println("学号：" + student.getId());
                System.out.println("姓名：" + student.getName());
                System.out.println("电话：" + student.getPhone());
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private void updateStudent() {
        while (true) {
            System.out.println("请输入要更新的学生学号（输入QUIT退出）：");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }

            Wym_Student student = studentMap.get(id);
            if (student == null) {
                System.out.println("未找到该学生。");
                continue;
            }

            System.out.println("请输入新的学生姓名：");
            String name = scanner.nextLine();
            System.out.println("请输入新的学生电话：");
            String phone = scanner.nextLine();

            Wym_Student updatedStudent = new Wym_Student(id, name, phone);
            studentMap.put(id, updatedStudent);
            System.out.println("学生信息更新成功。");
            saveStudents(); // 保存更新后的学生信息
        }
    }

    private void deleteStudent() {
        while (true) {
            System.out.print("请输入学号或输入'QUIT'退出: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            if (studentMap.remove(id) != null) {
                System.out.println("学生信息删除成功。");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            studentMap = (Map<String, Wym_Student>) ois.readObject();
        } catch (FileNotFoundException e) {
            System.out.println("学生信息文件不存在，将创建新文件。");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(studentMap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
